
class TimerUtil {
    node: cc.Node = null;
    init(node) {
        this.node = node;
    }
    //此函数不能再onLoad、onEnable函数调用
    start(action, callback: Function, node: cc.Node) {
        var node = node || this.node;
        if (typeof node == "undefined" || cc.isValid(node) == false) {
            return null;
        }

        var timer = { clear: null, sequence: null, node: node };
        if (typeof action == "number") {
            action = cc.delayTime(action)
        }
        var s = cc.sequence(action, cc.callFunc(function () {
            timer.sequence = null;
            timer.clear();
            if (callback)
                callback();
        }));
        timer.sequence = s;
        timer.clear = function () {
            if (typeof node == "undefined" || cc.isValid(node) == false) {
                return;
            }
            if (timer.sequence) {
                node.stopAction(timer.sequence);
                timer.sequence = null;
            }
            node.off("active-in-hierarchy-changed", timer.clear);
        }
        node.on("active-in-hierarchy-changed", timer.clear);

        node.runAction(timer.sequence);
        return timer;
    }

    stop(timer) {
        if (typeof timer == "undefined" || timer == null) {
            return;
        }
        var node = timer.node;
        if (typeof node == "undefined" || cc.isValid(node) == false) {
            return;
        }
        if (timer.sequence) {
            node.stopAction(timer.sequence);
            timer.sequence = null;
        }
        node.off("active-in-hierarchy-changed", timer.clear);
    }
}
export let timerUtil: TimerUtil = new TimerUtil();
